package app.beit.finportal.service.convert;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.NoSuchPaddingException;

import app.beit.finportal.bean.AccountBean;
import app.beit.finportal.entity.Account;
import app.beit.finportal.utils.FinportalEncryption;
import app.beit.finportal.utils.StringUtils;

public class AccountConvert {

	/**
	 * Convert Entity To Bean
	 * 
	 * @param entity
	 * @return AccountBean
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeySpecException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws InvalidKeyException 
	 */
	public static AccountBean entityToBean(final Account entity) throws InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
		final AccountBean bean = new AccountBean();

		bean.setId(entity.getId());
		bean.setCreatedBy(StringUtils.nullToEmpty(entity.getCreatedBy()));
		bean.setCreatedDate(entity.getCreatedDate());
		bean.setUpdatedBy(StringUtils.nullToEmpty(entity.getUpdatedBy()));
		bean.setUpdatedDate(entity.getUpdatedDate());
		bean.setStatus(entity.getStatus());
		bean.setEmail(StringUtils.nullToEmpty(entity.getEmail()));
		bean.setFullname(StringUtils.nullToEmpty(entity.getFullname()));
		bean.setPassword(FinportalEncryption.getInstance().decrypt(entity.getPassword()));
		bean.setRole(entity.getRole());
		bean.setTel(StringUtils.nullToEmpty(entity.getTel()));
		bean.setTitleName(StringUtils.nullToEmpty(entity.getTitleName()));
		bean.setUsername(entity.getUsername());

		return bean;
	}

	/**
	 * Convert Bean to Entity
	 * 
	 * @param bean
	 * @return Account
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeySpecException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws InvalidKeyException 
	 */
	public static Account beanToEntity(final AccountBean bean) throws InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
		final Account entity = new Account();

		entity.setId(bean.getId());
		entity.setCreatedBy(StringUtils.nullToEmpty(bean.getCreatedBy()));
		entity.setCreatedDate(bean.getCreatedDate());
		entity.setUpdatedBy(StringUtils.nullToEmpty(bean.getUpdatedBy()));
		entity.setUpdatedDate(bean.getUpdatedDate());
		entity.setStatus(bean.getStatus());
		entity.setEmail(StringUtils.nullToEmpty(bean.getEmail()));
		entity.setFullname(StringUtils.nullToEmpty(bean.getFullname()));
		entity.setPassword(FinportalEncryption.getInstance().encrypt(bean.getPassword()));
		entity.setRole(bean.getRole());
		entity.setTel(StringUtils.nullToEmpty(bean.getTel()));
		entity.setTitleName(StringUtils.nullToEmpty(bean.getTitleName()));
		entity.setUsername(bean.getUsername());

		return entity;
	}

	/**
	 * Convert List Entity to List Bean
	 * 
	 * @param entities
	 * @return List AccountBean
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeySpecException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws InvalidKeyException 
	 */
	public static List<AccountBean> entityToBean(final List<Account> entities) throws InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
		final List<AccountBean> beans = new ArrayList<AccountBean>();

		for (final Account entity : entities) {
			beans.add(AccountConvert.entityToBean(entity));
		}

		return beans;
	}

	/**
	 * Convert List Bean to List Entity
	 * 
	 * @param beans
	 * @return List Account
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeySpecException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws InvalidKeyException 
	 */
	public static List<Account> beanToEntity(final List<AccountBean> beans) throws InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
		final List<Account> entities = new ArrayList<Account>();

		for (final AccountBean bean : beans) {
			entities.add(AccountConvert.beanToEntity(bean));
		}

		return entities;
	}

}
